package effectivejava.avoidfloatdouble;

import java.math.BigDecimal;

public class AvoidFloatAndDoubleOriginal {
    public static void main(String[] args) {
        basicCalc();
        calcByDouble();
        calcByBigDecimal();
    }

    private static void calcByDouble() {
        double funds = 1;
        int itemsBought = 0;
        for (double price = 0.1; funds >= price; price += 0.1) {
            funds -= price;
            itemsBought++;
        }
        System.out.println("itemsBought:" + itemsBought);
        System.out.println("Change:" + funds);
    }

    private static void calcByBigDecimal() {
        BigDecimal funds = new BigDecimal("1");
        int itemsBought = 0;
        BigDecimal TEN_CENTS = new BigDecimal("0.1");
        for (BigDecimal price = TEN_CENTS; funds.compareTo(price) >= 0; price = price.add(TEN_CENTS)) {
            funds = funds.subtract(price);
            itemsBought++;
        }
        System.out.println("itemsBought:" + itemsBought);
        System.out.println("Change:" + funds);
    }

    private static void basicCalc() {
        System.out.println(1.03 - 0.42);
        System.out.println(1 - 9 * 0.1);
    }

}
